package com.yuanlong.controller;

import com.yuanlong.bean.Depart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/depart")
public class SomeController {

    @Autowired
    private RestTemplate restTemplate;
    // 直连提供者
//    private static final String SERVICES_PROVIDER = "http://localhost:8081";
    // 要使用微服务名称从Eureka Server查找提供者
    private static final String SERVICES_PROVIDER = "http://cyl-provider-depart";

    @PostMapping
    public void saveHandler(@RequestBody Depart depart){
        String url = SERVICES_PROVIDER + "/depart/";
        restTemplate.postForLocation(url, depart);
    }

    @DeleteMapping("/{id}")
    public void deleteHandler(@PathVariable("id") Integer id){
        String url = SERVICES_PROVIDER + "/depart/" + id;
        restTemplate.delete(url);
    }

    @PutMapping
    public void updateHandler(@RequestBody Depart depart){
        String url = SERVICES_PROVIDER + "/depart/";
        restTemplate.put(url, depart);
    }

    @GetMapping("/{id}")
    public Depart getByIdHandler(@PathVariable("id") Integer id){
        String url = SERVICES_PROVIDER + "/depart/" + id;
        return restTemplate.getForObject(url, Depart.class);
    }

    @GetMapping
    public List<Depart> listHandler(){
        String url = SERVICES_PROVIDER + "/depart/";
        return restTemplate.getForObject(url, List.class);
    }
}
